package com.deltabravomedia.theoldtoad.server;

import java.io.IOException;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.Blob;


@SuppressWarnings("serial")
public class GetImage extends HttpServlet {

    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    	// find desired image
        PersistenceManager pm = PMF.get().getPersistenceManager();
        Query query = pm.newQuery("select from "+ Image.class.getName() + " where name == '" 
        		+ req.getQueryString() + "'");
        List<Image> results = (List<Image>)query.execute(query);
        if (results.isEmpty()) {
        } else {
            for (Image result : results) {
                // serve the first image
                resp.setContentType("image/jpeg");
                resp.getOutputStream().write(result.getImage().getBytes());
                break;
            }
        }
	}
}